import{getThesaurusList,setThesaurusList,changeThesaurus,changeWordsInput} from '../../../../api/api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    arr:[],

    //白名单，1为白
    whiteFlag: 1,

    //词库信息
    lexiconList:[],

    //是否开启白名单true开启
    checked: false,

    //复选框是否选中1是0否
    isChoice: 1,
    
    lexiconId: null,
    name: '',


    result: [],
    option1: [
      { text: '白名单', value: 1 },
      { text: '机要词库', value: 2 },
      { text: '色情词库', value: 3 },
      { text: '暴恐词库', value: 4 },
      { text: '反对词库', value: 5 },
      { text: '贪腐词库', value: 6 },
      { text: '民生词库', value: 7 },
    ],
    option2: [
      { text: '白名单', value: 1 },
      { text: '机要词库', value: 2 },
      { text: '色情词库', value: 3 },
      { text: '暴恐词库', value: 4 },
      { text: '反对词库', value: 5 },
      { text: '贪腐词库', value: 6 },
      { text: '民生词库', value: 7 },
    ],
    lexiconName:'',
    value1: 1,
    value2: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    //获取获取词库信息
    getThesaurusList()
    .then(res=>{
      const mydata = res.data.data;
      const options = mydata.whiteFlag
      if(options == 1){
        this.setData({
          checked: true
        })
      }else{
        this.setData({
          checked: false
        })
      }
      this.setData({
        whiteFlag: mydata.whiteFlag,
        lexiconList: mydata.lexiconList
      })
    })
  },

  //是否开启白名单
  onChangeData(){
    if(this.data.checked == true){
      this.setData({
        checked: false,
        whiteFlag: 0
      })
      console.log("当前的值为1：" + this.data.whiteFlag);
    }else{
      this.setData({
        checked: true,
        whiteFlag: 1
      })
      console.log("当前的值为2：" + this.data.whiteFlag);
    }
  },

  //审核多选设置
  onChange(event) {
    const newArr=event.detail.value
    let oldArr=this.data.lexiconList
    for(let i=0;i<oldArr.length;i++){
      oldArr[i].isChoice=0
      for(let j=0;j<newArr.length;j++){
        if(oldArr[i].name==newArr[j]){
          oldArr[i].isChoice=1
        }
      }
    }
    this.setData({
      lexiconList:oldArr,
    });
  },

  //提交审核设置修改
  checkboxChange(){
    let oldArr=this.data.lexiconList
  
    // oldArr=oldArr.filter((item)=>item.isChoice==1)
    console.log('列表：',oldArr)
    setThesaurusList({
      whiteFlag: this.data.whiteFlag,
      lexiconList: oldArr
    })
    getThesaurusList().then(res=>{
      const mydata = res.data.data;
      const options = mydata.whiteFlag
      if(options == 1){
        this.setData({
          checked: true
        })
      }else{
        this.setData({
          checked: false
        })
      }
      this.setData({
        whiteFlag: mydata.whiteFlag,
        lexiconList: mydata.lexiconList
      })
    })
    
  },

  //文本添加/删除双向绑定
  textInput(value){
    let text = value.detail.value;
    const currentPage = getCurrentPages()[getCurrentPages().length - 1];
    const list = currentPage.data.lexiconList;
    const info = list.find(obj=> obj.name=== text)
    console.log("您输入的内容为：" + text);
    this.setData({
      lexiconName: text
    })
  },

  //词库添加提交
  lexiconSubmit(){
    console.log(this.data.lexiconName);
    changeThesaurus({
      name: this.data.lexiconName,
      flag: 1,
      id: null
    }).then(res=>{
      console.log(res);
      this.setData({
        lexiconName:''
      })
      this.onLoad()
      console.log("添加成功");
    })
    getThesaurusList().then(res=>{
      const mydata = res.data.data;
      const options = mydata.whiteFlag
      if(options == 1){
        this.setData({
          checked: true
        })
      }else{
        this.setData({
          checked: false
        })
      }
      this.setData({
        whiteFlag: mydata.whiteFlag,
        lexiconList: mydata.lexiconList
      })
      console.log("添加的数据为 ：" + mydata);
    })
  },

  //词库删除提交
  lexiconDelete(){
    console.log(this.data.lexiconName);
    changeThesaurus({
      name: this.data.lexiconName,
      flag: 0,
      id: null
    }).then(res=>{
      console.log(res);
      this.setData({
        lexiconName:''
      })
      this.onLoad()
      console.log("删除成功");
    })
    getThesaurusList().then(res=>{
      const mydata = res.data.data;
      const options = mydata.whiteFlag
      if(options == 1){
        this.setData({
          checked: true
        })
      }else{
        this.setData({
          checked: false
        })
      }
      this.setData({
        whiteFlag: mydata.whiteFlag,
        lexiconList: mydata.lexiconList
      })
    })
  },


  //删除敏感词绑定value值变化触发事件
  ChooseItems(e){
    const currentPage = getCurrentPages()[getCurrentPages().length - 1];
    let id = e.detail
    const option = currentPage.data.option1;
    const text = option.find(obj=> obj.value=== id)
    console.log(text.text);
    console.log(text.value);
    this.setData({
      lexiconId: text.value,
      name: text.text
    })
  },
  wordAdd(){
    changeWordsInput({
      id: this.data.lexiconId,
      name: this.data.name,
      flag: 1,
      word: this.data.lexiconName
    }).then(res=>{
      console.log(res.data.msg);
      const msg = res.data.msg
      //完成清楚data缓存
      this.setData({
        lexiconId:null,
        name: '',
        lexiconName: '',
        value2: 1,
        value1: 1
      })
      wx.showModal({
        title: '提示',
        content: msg,
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }).catch(res=>{
      console.log(res);
    })
  },
  wordDelete(){
    changeWordsInput({
      id: this.data.lexiconId,
      name: this.data.name,
      flag: 0,
      word: this.data.lexiconName
    }).then(res=>{
      console.log(res.data.msg);
      const msg = res.data.msg
      //完成清楚data缓存
      this.setData({
        lexiconId:null,
        name: '',
        lexiconName: '',
        value2: 1,
        value1: 1
      })
      wx.showModal({
        title: '提示',
        content: msg,
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }).catch(res=>{
      console.log(res);
    })
  },
  WordModel(){

  },
  DocumentModel(){
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  
  clickbtn(){
  },
  onReady() {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})